//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Globals.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
THexForm *HexForm;
//---------------------------------------------------------------------------
__fastcall THexForm::THexForm(TComponent* Owner)
 : TForm(Owner)
{
}

//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::DrawHex( BYTE *String, bool flag )
{
  WORD x,
       y,
       ptr,
       fnHeight;
  char buf[16];

  if ( !rom.data ) return;

  memset( hxData, 0, 0x1000 );

  // GET the BYTE count
  hexx.posi = 0;
  count     = 0;
  while( count < 0x1000 && String[count] != 0xFF )
  { //
    hxData[ count ] = String[ count ];
    count++;
    if ( *(WORD *)&String[ count ] == 0xFFFF ){
      hxData[ count ] = String[ count ]; count++;
      hxData[ count ] = String[ count ]; count++;
      break;
    }
  }

  //
  if ( !count && *(WORD *)&String[ count ] == 0xFFFF ){
    hxData[ count ] = String[ count ]; count++;
    hxData[ count ] = String[ count ]; count++;
  }

  //
  fnHeight = abs( HexForm->Font->Height ) + 2;
  //
  hexx.flag = flag;
  if ( hexx.mx != (8 << flag) )
    hexx.y = hexx.x = 0;
  //
  hexx.mx = 8 << flag;
  hexx.my = (HexForm->ClientHeight / fnHeight);
  // Re-adjust the window height
//  HexForm->ClientHeight = (hexx.my * fnHeight);
  //
  y   = -1;
  while( ++y < hexx.my )
  { //
    ptr = 0;
    x = -1;
    while( ++x < hexx.mx )
    { //
      if ( flag )
        sprintf( buf, "%02X", hxData[ hexx.posi + (y << 4) + x ] );
      else
        sprintf( buf, "%04X", *(WORD *)&hxData[ hexx.posi + (y << 4) + (x << 1) ] );
      //
      if ( x == hexx.x && y == hexx.y )
      { //
        HexForm->Canvas->Font->Color = clRed;
        SetBkColor( HexForm->Canvas->Handle, clWhite);
      }
      else
      { //
        HexForm->Canvas->Font->Color = ( x & 1 ) ? clWhite : clTeal;
        SetBkColor( HexForm->Canvas->Handle, clBlack );
      }

      //
			 HexForm->Canvas->TextOut( ptr, (y * fnHeight), buf );
      ptr += HexForm->Canvas->TextWidth( AnsiString(buf) );
    }
  }
  //
  DrawPic( flag );
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::UpdateHex( BYTE *String, bool flag )
{
  WORD x,
       y,
       ptr,
       fnHeight;
  char buf[16];

  if ( !rom.data ) return;
  //
  fnHeight  = abs( HexForm->Font->Height ) + 2;
  //
  hexx.flag = flag;
  if ( hexx.mx != (8 << flag) )
    hexx.y = hexx.x = 0;
  //
  hexx.mx   = (8 << flag);
  hexx.my   = (HexForm->ClientHeight / fnHeight);
  // Re-adjust the window height
//  HexForm->ClientHeight = (hexx.my * fnHeight);
  //
  y   = -1;
  while( ++y < hexx.my )
  { //
    ptr = 0;
    x = -1;
    while( ++x < hexx.mx )
    { //
      if ( flag )
        sprintf( buf, "%02X", hxData[ hexx.posi + (y << 4) + x ] );
      else
        sprintf( buf, "%04X", *(WORD *)&hxData[ hexx.posi + (y << 4) + (x << 1) ] );
      //
      if ( x == hexx.x && y == hexx.y )
      { //
        HexForm->Canvas->Font->Color = clRed;
        SetBkColor( HexForm->Canvas->Handle, clWhite);
      }
      else
      { //
        HexForm->Canvas->Font->Color = ( x & 1 ) ? clWhite : clTeal;
        SetBkColor( HexForm->Canvas->Handle, clBlack );
      }

      //
			 HexForm->Canvas->TextOut( ptr, (y * fnHeight), buf );
      ptr += HexForm->Canvas->TextWidth( AnsiString(buf) );
    }
  }
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormCreate(TObject *Sender)
{
  hexx.mx = 0;
  hexx.my = 0;
  hexx.x  = 0;
  hexx.y  = 0;

  PIC = new Graphics::TBitmap;

  PIC->Width       = 8;
  PIC->Height      = 16;
  PIC->PixelFormat = pf16bit;
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  switch( Key )
  {
    case VK_LEFT : if ( hexx.x > 0 ) hexx.x--; else { hexx.x = (hexx.mx-1); goto _Up; }   break;
    case VK_RIGHT: if ( hexx.x < (hexx.mx-1) ) hexx.x++; else { hexx.x = 0; goto _Down; } break;
    //------------
    case VK_UP   : _Up:
                    if ( hexx.y > 0 ){ hexx.y--; break; }
                    if ( !hexx.y  && hexx.posi ) hexx.posi -= (hexx.mx << (1 - hexx.flag));
                    break;
    //------------
    case VK_DOWN : _Down:
                  if ( hexx.y < (hexx.my-1) ){ hexx.y++; break; }
                  if ( hexx.y == (hexx.my-1) && (hexx.posi + hexx.mx) < 0x1000 ) hexx.posi += (hexx.mx << (1 - hexx.flag));
                  break;
    //----------------------
    default : return;
  }
  //
  UpdateHex( hxData, hexx.flag );
  DrawPic( hexx.flag );
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::DrawPic( bool flag )
{
  LONG posi;
  BYTE TILE[16][16];
  WORD *VRAM;
  BYTE BUFR[0x20];

  if ( !rom.data ) return;


  txt.data = hxData[ hexx.posi + (hexx.y << 4) + hexx.x];

  //----------------//
  // FONT is 8x16   //
  //----------------//
  if ( !myForm->DTT->Checked && !flag )
  { // Get to the next WORD
    txt.data = *(WORD *)&hxData[ hexx.posi + (hexx.y << 4) + (hexx.x<<1) ];
    if ( txt.data & 0xF000) txt.data = 0x0020;
    // Display the text
    txt.tpos  = ((txt.data & 0xFFFF) << 2);
    // Get the font map for TOP char
    txt.data  = WGET( rom.data, 0x9BDB + txt.tpos );
//    memcpy( &BUFR[0x20], &gFONT[ (txt.data << 4) ], 0x10 );
    // Get the font map for TOP char
    txt.data  = WGET( rom.data, 0x9BDD + txt.tpos );
//    memcpy( &BUFR[0x30], &gFONT[ (txt.data << 4) ], 0x10 );
    // Transfer the 1bpp data I want
    for ( int c = 0; c < 8; c++ ){
      BUFR[ 0x00 + c ] = BUFR[0x21 + (c << 1 ) ];
      BUFR[ 0x08 + c ] = BUFR[0x31 + (c << 1 ) ];
    }

    // Convert the tile data
    int x = -1;
    while( ++x < 16 )
      Dec2Bin( &txt.tile[ x << 3 ], BUFR[ x ], 8 );
    //
  }
  else
  //--------------------------//
  // My font used by the game //
  // 8x16 for control codes   //
  //--------------------------//
  {
    memcpy( BUFR, &FONT8x16[ (txt.data & 0xFF) << 4 ], 0x10 );
    memset( txt.tile, 0, 256 );
    // Convert the tile data
    int x = -1;
    while( ++x < 16 )
      Dec2Bin( &txt.tile[ x << 3 ], BUFR[ x ], 8 );
    //
  }

  //---------------//
  // DMA to Vram   //
  //---------------//
  for ( int y = 0; y < 16; y++ )
  { //
    (void *)VRAM = PIC->ScanLine[ y ];
    for ( int x = 0; x < 8; x++ )
      VRAM[ x ] = DOS565[ txt.tile[ (y << 3) + x ] * 0x0F ];
  }
  //
//  HexForm->Canvas->StretchDraw( Rect( 264, 4, 390, 128 ), PIC );
  chPic->Canvas->StretchDraw( chPic->ClientRect, PIC );
  //
  // Display the Offset
  if ( (hexx.posi + (hexx.y << 3) + hexx.x) < (count-1) )
    HexForm->Caption = "Hex Data - $" + IntToHex( (0x7E0000 | txt.pntr) + (hexx.posi + (hexx.y << 4) + (hexx.x << 1)), 8 );
  else
    HexForm->Caption = "Hex Data - $" + IntToHex( (0x7E0000 | txt.pntr) + (count-1), 8 );
}

//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormPaint(TObject *Sender)
{
  UpdateHex( hxData, hexx.flag );
  DrawPic( hexx.flag );
}


void __fastcall THexForm::FormResize(TObject *Sender)
{
  FormPaint( Sender ); 
}
//---------------------------------------------------------------------------

